package com.goods.controller.business;

import com.goods.business.service.ProductService;
import com.goods.common.model.business.Product;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.ProductStockVO;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/business/product")
public class productController {

    @Autowired
    private ProductService productService;


    /**
     * 物资资料分页列表
     * @param pageNum  起始页
     * @param pageSize 每页显示条数
     * @param name     查询 name
     * @param categoryId  分类 ID
     * @param status     状态
     * @return
     */
    //pageNum=1&pageSize=6&name=&categoryId=&supplier=&status=0
    @GetMapping("/findProductList")
    public ResponseBean findProductList(@RequestParam("pageNum")int pageNum,
                                        @RequestParam("pageSize")int pageSize,
                                        @RequestParam("name") String name,
                                        @RequestParam("categoryId") Integer categoryId,
                                        @RequestParam("status") Integer status){
        try {
            //调用方法，返回参数
            PageVO<ProductVO> pageVO = productService.findProductList(pageNum,pageSize,name,categoryId,status);
            if (pageVO != null){
                return ResponseBean.success(pageVO);
            }
            return ResponseBean.error("查询失败");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBean.error("查询失败");
        }
    }

    /**
     * 添加物资
     * @param productVO
     * @return
     */
    @PostMapping("/add")
    public ResponseBean add(@RequestBody ProductVO productVO ){
        try {
            productService.add(productVO);
            return ResponseBean.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBean.error("添加失败");
        }
    }

    /**
     * 回显数据
     * @param id
     * @return
     */
    @GetMapping("/edit/{id}")
    public ResponseBean edit(@PathVariable Long id){
        Product product = productService.edit(id);
        if (product != null){
            return ResponseBean.success(product);
        }
       return ResponseBean.error("回显失败");
    }

    /**
     * 放入回收站
     * @param id
     * @return
     */
    @PutMapping("remove/{id}")
    public ResponseBean remove(@PathVariable Long id){
        try {
            productService.remove(id);
            return ResponseBean.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBean.error("放入回收站错误");
        }
    }


    /**
     * 恢复回收站
     * @param id
     * @return
     */
    @PutMapping("back/{id}")
    public ResponseBean back(@PathVariable Long id){
        try {
            productService.back(id);
            return ResponseBean.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBean.error("恢复回收站错误");
        }
    }

    /**
     * 删除回收站
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public ResponseBean delete(@PathVariable Long id){
        try {
            productService.delete(id);
            return ResponseBean.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBean.error("恢复回收站错误");
        }
    }

    /**
     * 审核通过回收站
     * @param id
     * @return
     */
    @PutMapping("publish/{id}")
    public ResponseBean publish(@PathVariable Long id){
        try {
            productService.back(id);
            return ResponseBean.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBean.error("恢复回收站错误");
        }
    }

    /**
     * 查询所有物资
     */
    @GetMapping("/findProducts")
    public ResponseBean findProduct(@RequestParam("pageNum")int pageNum,
                                        @RequestParam("pageSize")int pageSize,
                                        @RequestParam("status") Integer status){
        try {
            //调用方法，返回参数
            PageVO<ProductStockVO> pageVO = productService.findProducts(pageNum,pageSize,status);
            if (pageVO != null){
                return ResponseBean.success(pageVO);
            }
            return ResponseBean.error("查询失败");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBean.error("查询失败");
        }
    }


    /**
     * 物资库存图
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/findProductStocks")
    public ResponseBean findProductStocks(@RequestParam("pageNum")int pageNum,
                                          @RequestParam("pageSize")int pageSize){
        try {
            PageVO<ProductStockVO> PageVO = productService.findProductStocks(pageNum,pageSize);
            return ResponseBean.success(PageVO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBean.error("查询失败");
        }
    }


    /**
     * 获取所有库存
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/findAllStocks")
    public ResponseBean findAllStocks(@RequestParam("pageNum")int pageNum,
                                      @RequestParam("pageSize")int pageSize){
        try {
            List<Map<String,Object>> list = productService.findAllStocks(pageNum,pageSize);
            return ResponseBean.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBean.error("获取所有库存失败");
        }
    }
}
